from django.shortcuts import render, get_list_or_404, redirect
from shopping.models import Food_vegetables #Food_fruit,Food_sea,Food_meat,Food_eggs,Food_quick,
from django.views.generic import TemplateView, DetailView
from users.forms import User_logforms, User_regforms, User_otherforms
from django.http import HttpResponse,HttpResponseRedirect
from user_operations.forms import CommentForm
from user_operations.models import Comments
from django.urls import reverse_lazy,reverse
from django.db.models import Q

# from django.contrib.auth import get_user_model

# Create your views here.
# def index_html(requset):
#
#     food_sea = Food_sea.objects.all().order_by('-id')
#     food_meat = Food_meat.objects.all().order_by('-id')
#     food_eggs = Food_eggs.objects.all().order_by('-id')
#     food_quick = Food_quick.objects.all().order_by('-id')
#     food_vegetables = Food_vegetables.objects.all().order_by('-id')
#     food_fruit = Food_fruit.objects.all().order_by('-id')
#     return render(requset, 'shopping/index.html', {'food_seas':food_sea, 'food_meats':food_meat, 'food_eggss':food_eggs,
#                                                    'food_quicks':food_quick, 'food_vegetabless':food_vegetables, 'food_fruits':food_fruit,})

#类视图
# User = get_user_model()
#类视图
class Index_html(TemplateView):
    """
    帮助我们渲染模版
    """
    template_name = 'shopping/index.html'
    def get_context_data(self, **kwargs):
    #     if 'view' not in kwargs:
    #         kwargs['view'] = self
    #     return kwargs
    #     food_sea = Food_sea.objects.all().order_by('-id')
    #     food_meat = Food_meat.objects.all().order_by('-id')
    #     food_eggs = Food_eggs.objects.all().order_by('-id')
    #     food_quick = Food_quick.objects.all().order_by('-id')

    #     food_fruit = Food_fruit.objects.all().order_by('-id')
    #     food_vegetables = Food_vegetables.objects.all().order_by('-id')
        fruits = Food_vegetables.objects.filter(kind__name="新鲜水果")

        seas = Food_vegetables.objects.filter(kind__name="海鲜水产")

        meats = Food_vegetables.objects.filter(kind__name="猪牛羊肉")

        eggs = Food_vegetables.objects.filter(kind__name="禽类蛋品")

        vegetables = Food_vegetables.objects.filter(kind__name="新鲜蔬菜")

        quicks = Food_vegetables.objects.filter(kind__name="速冻食品")
        kwargs['fruits'] = fruits
        kwargs['seas'] = seas
        kwargs['meats'] = meats
        kwargs['eggs'] = eggs
        kwargs['vegetables'] = vegetables
        kwargs['quicks'] = quicks

        # kwargs['food_seas'] = food_sea
        # kwargs['food_meats'] = food_meat
        # kwargs['food_eggss'] = food_eggs
        # kwargs['food_quicks'] = food_quick
        # kwargs['food_vegetabless'] = food_vegetables
        # kwargs['food_fruits'] = food_fruit
        return super().get_context_data(**kwargs)





# def get_register_html(request):
#     return render(request, "shopping/register.html")

class Get_register_html(TemplateView):
    template_name = "shopping/register.html"

    def get_context_data(self, **kwargs):
        # if 'view' not in kwargs:
        #     kwargs['view'] = self
        # return kwargs
        forms = User_regforms()
        kwargs["forms"] = forms
        return super().get_context_data(**kwargs)



# def get_login_html(request):
#
#     # x = 11
#     return render(request, "shopping/login.html")

class Get_login_html(TemplateView):
    template_name = "shopping/login.html"
    def get_context_data(self, **kwargs):
        # if "view" not in kwargs:
        #     kwargs["view"] = self
        # return kwargs
        forms = User_logforms()
        kwargs["forms"] = forms
        return super().get_context_data(**kwargs)




# def get_user_center_info_html(request):
#     forms = User_otherforms()
#
#     return render(request, "shopping/user_center_info.html")

class Get_user_center_info_html(TemplateView):
    # template_name = "shopping/user_center_info.html"
    def get(self, request, *args, **kwargs):
        if self.request.user.id == None:
            url = reverse_lazy("login")
            # print(1)
            return redirect(url)
        else:
            form = User_otherforms()
            # kwargs["form"] = form
            return render(request,"shopping/user_center_info.html",{"form":form})
    # def get_context_data(self, **kwargs):
    #     # if "view" not in kwargs:
    #     #     kwargs["view"] = self
    #     # return kwargs
    #     # kwargs[]
    #
    #     forms = User_otherforms()
    #     kwargs["form"] = forms
    #     return super().get_context_data(**kwargs)



# def get_cart_html(request):
#     return render(request, "shopping/cart.html")

class Get_cart_html(TemplateView):
    template_name = "shopping/cart.html"

    def get(self, request, *args, **kwargs):
        if self.request.user.id == None:
            url = reverse_lazy("login")
            # print(1)
            return redirect(url)
        # if "view" not in kwargs:
        #     kwargs["view"] = self
        # return kwargs
        else:
            return render(request, "shopping/cart.html")



# def get_user_center_order(request):
#     return render(request, "shopping/user_center_order.html")

class Get_user_center_order(TemplateView):
    template_name = "shopping/user_center_order.html"

    def get(self, request, *args, **kwargs):
        if self.request.user.id == None:
            url = reverse_lazy("login")
            # print(1)
            return redirect(url)
        # if "view" not in kwargs:
        #     kwargs["view"] = self
        # return kwargs
        else:
            return render(request, "shopping/user_center_order.html")

# def get_user_center_site_html(request):
#     return render(request, "shopping/user_center_site.html")

class Get_user_center_site_html(TemplateView):
    template_name =  "shopping/user_center_site.html"
    # def get(self, request, *args, **kwargs):

    def get_context_data(self, **kwargs):
        # if "view" not in kwargs:
        #     kwargs["view"] = self
        # return kwargs
        return super().get_context_data(**kwargs)





# def get_detail_html(request,pk):
#     # try:
#     #     food_fruit = Food_fruit.objects.filter(id=2)[0]
#     # except IndexError:
#     #     return HttpResponse(status=404)
#     # food_fruit = get_list_or_404(Food_sea.objects.filter(id=pk))
#     # food_fruit = food_fruit[0]
#     food_vegetable = get_list_or_404(Food_vegetables.objects.filter(id=pk))
#     food_vegetable =food_vegetable[0]
#     return render(request, "shopping/detail.html",{'food_vegetable':food_vegetable})

class Get_detail_html(DetailView):
    queryset = Food_vegetables.objects.all()
    template_name = "shopping/detail.html"
    context_object_name = "food_vegetable"
    def get_context_data(self, **kwargs):
        kwargs["comment_form"] = CommentForm
        kwargs["comments"] = Comments.objects.filter(commodity=self.request.path.split("/")[3])
        return super().get_context_data(**kwargs)




class Get_list_html(TemplateView):
    template_name = "shopping/list.html"
    def get_context_data(self, **kwargs):
        fruits = Food_vegetables.objects.filter(kind__name="新鲜水果")

        seas = Food_vegetables.objects.filter(kind__name="海鲜水产")

        meats = Food_vegetables.objects.filter(kind__name="猪牛羊肉")

        eggs = Food_vegetables.objects.filter(kind__name="禽类蛋品")

        vegetables = Food_vegetables.objects.filter(kind__name="新鲜蔬菜")

        quicks = Food_vegetables.objects.filter(kind__name="速冻食品")
        kwargs['fruits'] = fruits
        kwargs['seas'] = seas
        kwargs['meats'] = meats
        kwargs['eggs'] = eggs
        kwargs['vegetables'] = vegetables
        kwargs['quicks'] = quicks
        return super().get_context_data(**kwargs)



def load_image(request):
    file = request.FILES.get("fileUpload").file
    with open("/Users/admin/Desktop/TTSX/media/commodity/test1.png","wb+") as f:
        f.write(file.getvalue())
    return HttpResponse("成功")



def image(request):
    return render(request, "shopping/load.html")

def search(request):
    q = request.GET.get("q")
    error_msg = ''
    if not q:
        return HttpResponse("请输入关键字")

    food_vegetables = Food_vegetables.objects.filter(Q(name__contains=q))

    return render(request, 'shopping/datail1.html', {
        'food_vegetables': food_vegetables

    })
